package supercafe.core;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


public class DataPoster extends AsyncTask<String, String ,Void> {
    private String responsedResult="";
    Activity ac;
    public DataPoster(Activity ac){
    	this.ac = ac;
    }
    @Override
    protected Void doInBackground(String... params) {
    	String xml = params[0];
    	String url = params[1];
    	String datatype = params[2];
          InputStream content = null;
          try {
        	 HttpClient httpclient = new DefaultHttpClient();
        	 HttpPost httppost = new HttpPost(url);
        	 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
             nameValuePairs.add(new BasicNameValuePair(datatype,xml));
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
             
        	 HttpResponse response = httpclient.execute(httppost);
            content = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String temp;
            while((temp = reader.readLine())!=null){
            	responsedResult+=temp;
            }
          } catch (Exception e) {
                Log.e("[GET REQUEST]", "Network exception");
          }
          
          return(null);
    }
    @Override
    protected void onProgressUpdate(String... values) {
    	
    }
    @Override
    protected void onPostExecute(Void result) {
        //TextView btn=(TextView)findViewById(R.id.button);
        //btn.setText(ss);
    	Toast.makeText(ac, responsedResult, Toast.LENGTH_LONG).show();
    }
    
    public String getResponseResult(){
    	return this.responsedResult;
    }
}